package com.sinodata.bsm.cicp.collector.linuxmq;

import java.io.BufferedReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.log4j.Logger;

import com.sinodata.bsm.cicp.collector.Collector;
import com.sinodata.bsm.cicp.exception.CollectorException;
import com.sinodata.bsm.cicp.util.CommandReader;
import com.sinodata.bsm.common.vo.ResPropertyValue;

/**
 * MQ通道采集器
 */
public class MqChannelCollector extends Collector {

    private static Logger logger = Logger.getLogger(MqChannelCollector.class.getName());

    @Override
    public List<ResPropertyValue> collect(String ip) throws CollectorException {
        List<ResPropertyValue> values = new ArrayList<ResPropertyValue>();
        String[] propMetas = getPropMeta();
        ResPropertyValue propertyValueX = new ResPropertyValue();
        ResPropertyValue.PropValue[] propValues = new ResPropertyValue.PropValue[propMetas.length];
        BufferedReader reader = null;
        String mqName = getParamMap().get("mqName");
        String chName = getParamMap().get("chName");
        String cmdString = "echo \"dis chs(" + chName + ")\"  | runmqsc " + mqName;
        //解析通道状态
        String statusRegex = ".*\\s+STATUS\\((\\S+)\\).*";
        //解析通道未使用
        String amqRegex = ".*\\s*AMQ8420.*";
        String status = null;
        try {
            reader = CommandReader.getReader(ip, cmdString, 0, 0);
            String line = null;
            Matcher match = null;
            Pattern p = null;
            while ((line = reader.readLine()) != null) {
                p = Pattern.compile(amqRegex);
                match = p.matcher(line);
                if (match.matches()) {
                    status = "no used";
                    break;
                }
                p = Pattern.compile(statusRegex);
                match = p.matcher(line);
                if (match.matches()) {
                    status = match.group(1);
                    if (!"RUNNING".equals(status)) {
                        break;
                    }
                }
            }
            propValues[0] = ResPropertyValue.createPropValue(status, propMetas[0]);
            propertyValueX.setValues(propValues);
            values.add(propertyValueX);
            return values;
        } catch (Exception e) {
            logger.error(e);
            throw new CollectorException(e);
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    logger.error(e);
                }
            }
        }
    }

    @Override
    public String[] getParamMeta() {
        return new String[] { "mqName", "chName" };
    }

    @Override
    public String[] getPropMeta() {
        //        return new String[] { "chState" };
        return new String[] { "$1" };
    }

}
